package com.xywy.bloodandroid.act;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.hp.hpl.sparta.Text;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.Article;
import com.xywy.bloodandroid.bean.BaseHttpResponse;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.DateUtil;
import com.xywy.bloodandroid.util.MD5;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;
import com.xywy.bloodandroid.vollery.VolleyManager;

/*******************************************
 * @Title: ArticleDetailActivity.java
 * @Package com.xywy.bloodandroid.act
 * @Description: TODO 血压咨询文章详情
 * @author Benwolf
 * @date 2014-12-29 上午11:25:38
 * @version V1.0
 ********************************************/
public class ArticleDetailActivity extends BaseActivity {

	private RelativeLayout back;
	private TextView title;
	private String dirStr;
	private String id;
	private TextView tv_detail_title;
	private TextView tv_detail_pubdate;
	private TextView tv_detail_description;
	private String typeid;
	private ImageView right_img;
	private boolean isCollected;

	@Override
	protected void initView(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.ativity_article_detail);
		back = (RelativeLayout) findViewById(R.id.left_img);
		title = (TextView) findViewById(R.id.middle_title);
		right_img = (ImageView) findViewById(R.id.right_img);
		Article article = new Article();
		tv_detail_title = (TextView) findViewById(R.id.tv_detail_title);
		tv_detail_pubdate = (TextView) findViewById(R.id.tv_detail_pubdate);
		tv_detail_description = (TextView) findViewById(R.id.tv_detail_description);
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		back.setOnClickListener(this);
		right_img.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.right_img:
			if (isCollected) {
				cancelCollected();
			} else {
				AddCollection();
			}

			break;

		default:
			break;
		}
	}

	/**
	 * 取消收藏
	 */
	private void cancelCollected() {
		// TODO Auto-generated method stub
		String url = "http://api.yun.xywy.com/index.php/app/collect/post/";
		PostRequest<BaseHttpResponse> cancelCollectPost = new PostRequest<BaseHttpResponse>(
				url, BaseHttpResponse.class,
				new OnVolleyResponseListener<BaseHttpResponse>() {

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
						Toast.makeText(context, "收藏失败！", Toast.LENGTH_SHORT)
								.show();
					}

					@Override
					public void onSuccess(BaseHttpResponse response) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
						// try {
						// JSONObject json = new JSONObject(response);
						// int state = json.optInt("state");
						if (response.getState() == 200) {
							isCollected = false;
							right_img.setBackground(getResources().getDrawable(
									R.drawable.img_collect));
							Toast.makeText(context, "取消收藏成功！",
									Toast.LENGTH_SHORT).show();
						}
						// } catch (JSONException e) {
						// // TODO Auto-generated catch block
						// e.printStackTrace();
						// }

					}
				});
		HashMap<String, String> params = new HashMap<String, String>();
		String sign;
		try {
			sign = MD5.md5s("KHy69gsk8%#@kl$" + "zj");
			params.put("sign", sign);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		params.put("tag", "zj");
		params.put("xywy_userid", User.getInstance(context).getUserid());
		params.put("app_id", 2 + "");
		// params.put("meta", value);
		params.put("categoryid", id);
		// params.put("document_id", id);
		params.put("title", tv_detail_title.getText().toString());
		params.put("category", "0");

		cancelCollectPost.setParams(params);
		executeRequest(cancelCollectPost);

	}

	/**
	 * 添加收藏
	 */
	private void AddCollection() {
		// TODO Auto-generated method stub
		String url = "http://api.yun.xywy.com/index.php/app/collect/post/";
		PostRequest<BaseHttpResponse> addCollectPost = new PostRequest<BaseHttpResponse>(
				url, BaseHttpResponse.class,
				new OnVolleyResponseListener<BaseHttpResponse>() {

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
						Toast.makeText(context, "收藏失败！", Toast.LENGTH_SHORT)
								.show();
					}

					@Override
					public void onSuccess(BaseHttpResponse response) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
						if (response.getState() == 200) {
							isCollected = true;
							right_img.setBackground(getResources().getDrawable(
									R.drawable.img_collected));
							Toast.makeText(context, "收藏成功！", Toast.LENGTH_SHORT)
									.show();
						}
					}
				});
		HashMap<String, String> params = new HashMap<String, String>();
		String sign;
		try {
			sign = MD5.md5s("KHy69gsk8%#@kl$" + "zj");
			params.put("sign", sign);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		params.put("tag", "zj");
		params.put("xywy_userid", User.getInstance(context).getUserid());
		params.put("app_id", 2 + "");
		params.put("meta", dirStr + "," + typeid);
		params.put("categoryid", id);
		// params.put("document_id", id);
		params.put("title", tv_detail_title.getText().toString());
		params.put("category", "1");

		addCollectPost.setParams(params);
		executeRequest(addCollectPost);

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		title.setText(getResources().getString(R.string.article_str));
		right_img.setBackground(getResources().getDrawable(
				R.drawable.img_collect));
		right_img.setVisibility(View.VISIBLE);
		dirStr = getIntent().getStringExtra("dirStr");
		typeid = getIntent().getStringExtra("typeid");
		id = getIntent().getStringExtra("id"); // getArticleDetail(dir, id);
		String URL = Constants.getArticleListUrl(Constants.BP_NEWS_DETAIL_URL);
		StringBuilder url = new StringBuilder();
		url.append(URL);
		url.append("&id=" + id);
		url.append("&dir=" + dirStr);

		GetRequest<String> articleDetailsRequest = new GetRequest<String>(
				url.toString(), String.class,
				new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
					}

					@Override
					public void onSuccess(String response) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
						JSONObject json;
						try {
							json = new JSONObject(response);
							int code = json.optInt("code");
							if (code == 10000) {
								JSONObject article = json.getJSONObject("data");
								String id = article.optString("id");
								System.out.println("id =========" + id);
								tv_detail_title.setText(article
										.optString("title"));
								tv_detail_description.setText(Html
										.fromHtml(article.optString("body")));
								tv_detail_pubdate.setText(DateUtil
										.getData(article.optString("pubdate")));
								article.optString("url");
							}
							;

						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				});
		articleDetailsRequest.setIsParseJson(false);
		VolleyManager.addRequest(articleDetailsRequest, this);
		executeRequest(articleDetailsRequest);
		checkIsCollected();
	}

	/**
	 * 检查收藏状态
	 */
	private void checkIsCollected() {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		String url = "http://api.yun.xywy.com/index.php/app/collect/is_collected/";
		PostRequest<String> checkIsCollect = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
						Toast.makeText(context, "检查收藏状态失败！", Toast.LENGTH_SHORT)
								.show();
					}

					@Override
					public void onSuccess(String response) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
						Toast.makeText(context, "检查收藏状态成功！", Toast.LENGTH_SHORT)
								.show();
						try {
							JSONObject json = new JSONObject(response);
							int state = json.optInt("state");
							int data = json.optInt("data");
							if (state == 200) {
								if (data == 1) {
									isCollected = true;
									right_img.setBackground(getResources()
											.getDrawable(
													R.drawable.img_collected));
								} else if (data == 0) {
									isCollected = false;
									right_img
											.setBackground(getResources()
													.getDrawable(
															R.drawable.img_collect));
								}
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}

				});
		HashMap<String, String> params = new HashMap<String, String>();
		String sign;
		try {
			sign = MD5.md5s("KHy69gsk8%#@kl$" + "zj");
			params.put("sign", sign);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		params.put("tag", "zj");
		params.put("xywy_userid", User.getInstance(context).getUserid());
		params.put("app_id", 2 + "");
		// params.put("meta", value);
		params.put("categoryid", id);
		// params.put("document_id", id);//医生id
		// params.put("title", tv_detail_title.getText().toString());
		// params.put("category", "0");
		checkIsCollect.setIsParseJson(false);
		checkIsCollect.setParams(params);
		executeRequest(checkIsCollect);

	}

}
